#!/bin/bash -eu

# 使用方法
# sh -c "$(curl -fsSL https://gitee.com/saymarsayeille/git-template/raw/master/tools/install.sh)"
# 或
# sh -c "$(wget -qO- https://gitee.com/saymarsayeille/git-template/raw/master/tools/install.sh)"

# Default settings
INSTALL_PATH="${INSTALL_PATH:-$HOME/git-template}"
GIT_REMOTE=${GIT_REMOTE:-https://gitee.com/saymarsayeille/git-template.git}
GIT_BRANCH=${GIT_BRANCH:-master}

# 以下代码来自 mhmyzsh/tools/install.sh

# The [ -t 1 ] check only works when the function is not called from
# a subshell (like in `$(...)` or `(...)`, so this hack redefines the
# function at the top level to always return false when stdout is not
# a tty.
if [ -t 1 ]; then
  is_tty() {
    true
  }
else
  is_tty() {
    false
  }
fi

# Adapted from code and information by Anton Kochkov (@XVilka)
# Source: https://gist.github.com/XVilka/8346728
supports_truecolor() {
#   case "$COLORTERM" in
#   truecolor|24bit) return 0 ;;
#   esac

  case "$TERM" in
  iterm           |\
  tmux-truecolor  |\
  linux-truecolor |\
  xterm-truecolor |\
  screen-truecolor) return 0 ;;
  esac

  return 1
}


setup_color() {
  # Only use colors if connected to a terminal
  if ! is_tty; then
    FMT_RAINBOW=""
    FMT_RED=""
    FMT_GREEN=""
    FMT_YELLOW=""
    FMT_BLUE=""
    FMT_BOLD=""
    FMT_RESET=""
    return
  fi

  if supports_truecolor; then
    FMT_RAINBOW="
      $(printf '\033[38;2;255;0;0m')
      $(printf '\033[38;2;255;97;0m')
      $(printf '\033[38;2;247;255;0m')
      $(printf '\033[38;2;0;255;30m')
      $(printf '\033[38;2;77;0;255m')
      $(printf '\033[38;2;168;0;255m')
      $(printf '\033[38;2;245;0;172m')
    "
  else
    FMT_RAINBOW="
      $(printf '\033[38;5;196m')
      $(printf '\033[38;5;202m')
      $(printf '\033[38;5;226m')
      $(printf '\033[38;5;082m')
      $(printf '\033[38;5;021m')
      $(printf '\033[38;5;093m')
      $(printf '\033[38;5;163m')
    "
  fi

  FMT_RED=$(printf '\033[31m')
  FMT_GREEN=$(printf '\033[32m')
  FMT_YELLOW=$(printf '\033[33m')
  FMT_BLUE=$(printf '\033[34m')
  FMT_BOLD=$(printf '\033[1m')
  FMT_RESET=$(printf '\033[0m')
}

fmt_error() {
  printf '%sError: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "$FMT_RESET" >&2
}

#
# git template 安装相关
config_shell()
{
    CWD=`realpath $INSTALL_PATH`
    SCRIPT_PATH="export PATH=$CWD/scripts:\$PATH"

    echo "$(basename -- "$SHELL")"

    case "$(basename -- "$SHELL")" in
    zsh)
        filename=".zshrc"
        ;;
    bash)
        filename=".bashrc"
        ;;
    *)
        fmt_error "Current shell is not supported."
        exit 1
        ;;
    esac

    case `grep -Fx "$SCRIPT_PATH" ~/$filename >/dev/null; echo $?` in
    0)
        # code if found
        ;;
    1)
        # code if not found
        echo "" >> ~/$filename
        echo "$SCRIPT_PATH" >> ~/$filename
        ;;
    *)
        # code if an error occurred
        ;;
    esac
}

config_git()
{
    CWD=`realpath $INSTALL_PATH`
    git config --global init.templatedir "$CWD/template"
}

install_template()
{
  umask g-w,o-w

  echo "${FMT_BLUE}Cloning git template...${FMT_RESET}"

  git init --quiet "$INSTALL_PATH" && cd "$INSTALL_PATH" \
  && git config core.eol lf \
  && git config core.autocrlf false || {
    [ ! -d "$INSTALL_PATH" ] || {
      cd -
      rm -rf "$INSTALL_PATH" 2>/dev/null
    }
    fmt_error "git clone repo failed"
    exit 1
  }

  git remote add origin "$GIT_REMOTE" 2>/dev/null || {
    git remote remove origin
    git remote add origin "$GIT_REMOTE" || {
        fmt_error "git remote update failed"
        exit 1
    }
  }

  git fetch --depth=1 origin \
  && git checkout -b "$GIT_BRANCH" "origin/$GIT_BRANCH"

  # Exit installation directory
  cd -

  echo
}

setup_color
install_template
config_shell
config_git
echo "Done!"
